package cn.edu.scau.sec.rw.model.disk_file.utils;

import cn.edu.scau.sec.rw.model.disk_file.file.ExeFile;
import cn.edu.scau.sec.rw.model.pro_manage.simulation.CPU;

/**
 * 可执行文件指令工具类
 */
public class ExeUtil {

    //假设在一个时间片内可以执行20条操作，时间片为6，一条指令就执行0.3
    public static int execute(CPU cpu, int x, String statement){

        if (statement.contains("x=")){
            statement = statement.replaceFirst("x=","");
            x = Integer.parseInt(statement);

        } else if ("end".equals(statement)) {
            System.out.println("执行结束：" + x);
            cpu.getRegister().getPSW()[0] = 1;

        } else if ("x++".equals(statement)){
            x++;
            System.out.println("执行中间结果：" + x);

        } else if ("x--".equals(statement)){
            x--;
            System.out.println("执行中间结果：" + x);

        } else if ('!' == statement.charAt(0)) {
            String name = String.valueOf(statement.charAt(1));
            Integer time = Integer.parseInt(statement.substring(2)); //去除前两位
            cpu.getRegister().getPSW()[2] = 1;
            cpu.getProcess().getPCB().setBlockReason("IO中断");
            cpu.getProcess().setDeviceName(name);
            cpu.getProcess().setDeviceTime(time);

        } else {
            System.out.println("错误或无效的指令！");
        }
        return x;
    }
}
